#!/bin/bash

WRITEQUOTA="/${QUOTA_TEST}/writeQuotaDir"
WRITEDIR="${FS_MOUNTPOINT}${WRITEQUOTA}"
LOGFILE="/tmp/quotalog"
global_used=0

msg0="测试所有用例";
msg1="测试在<空配额目录>新建文件且不超出配额";
msg2="测试在<空配额目录>新建文件且超出配额";
msg3="测试在<空配额目录>对已有文件覆盖写且不超出配额";
msg4="测试在<空配额目录>对已有文件覆盖写且超出配额";
msg5="测试在<空配额目录>对已有文件追加写且不超出配额";
msg6="测试在<空配额目录>对已有文件追加写且超出配额";
maxmsg=6

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${maxmsg}; num++ ));do
        eval echo "'    'sh ${name} ${num} '  ' \${msg${num}}"
    done
    exit
}

function init_all() {
    set -e
    hard_limit=$(($1 * 1024 * 1024)) # 10MB
    clean_quota;
    echo -n "=====清空目录并重新创建配额====="
    mkdir -p ${WRITEDIR}; rm -f ${WRITEDIR}/*;
    orcafs-ctl --setquota --dir=${WRITEDIR} --hardlimit=${hard_limit} &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};

    quota_num=`orcafs-ctl --listquota | grep -w DIR | grep ${WRITEQUOTA} | wc -l`
    if ((${quota_num} == 1));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
    set +e
}

function test_create_newfile() {
    rm -rf ${WRITEDIR}/*
    bs_size=$(($1 *1024 *1024))
    echo "测试清空目录并新建 $1 MB 文件"

    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";
        echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> /dev/null
    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} == 0)) && (( ${new_used} == ${bs_size})); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_create_newfile_exceed() {
    rm -rf ${WRITEDIR}/*
    bs_size=$(($1 *1024 *1024))
    echo "测试清空目录并新建 $1 MB 文件 (超出配额)"

    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size} count=1";
        echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> ${LOGFILE}
    
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
        if (( ${exceed} == 1 )); then echo -e "\t Error: Disk quota exceeded";fi
    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} == 0)) && (( ${new_used} == 0)) && (( ${exceed} == 1 )); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_open_with_trnuc() {
    rm -rf ${WRITEDIR}/*
    bs_size1=$(($1 *1024 *1024)) # 1MB
    bs_size2=$((2 *1024 *1024))  # 2MB
    echo "测试清空目录并新建 $1 MB 文件, 然后覆盖写 ${bs_size2}  (不超出配额)"
    
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size1} count=1"; echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
#test open with trunc
    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size2} count=1"; echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} == ${bs_size1})) && (( ${new_used} == ${bs_size2})); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_open_with_trnuc_exceed() {
    file_size=$1
    rm -rf ${WRITEDIR}/*
    bs_size1=$(($1 *1024 *1024))  # 1MB
    bs_size2=$((12 *1024 *1024))  # 12MB
    echo "测试清空目录并新建 $1 MB 文件, 然后覆盖写 ${bs_size2}  (超出配额)"
    
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size1} count=1";echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
    #test
    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size2} count=1";echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> ${LOGFILE}
    
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`; 
        if (( ${exceed} == 1 )); then echo -e "\t Error: Disk quota exceeded";fi
    
    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} == ${bs_size1})) && (( ${new_used} == 0)) && (( ${exceed} == 1 )); then
        echo -e "success exceed\n"; else echo -e "fail\n"; exit 1; fi
}

function test_append() {
    file_size=$1
    rm -rf ${WRITEDIR}/*
    bs_size1=$(($1 *1024 *1024))
    bs_size2=$((${bs_size1} * 2))
    echo "测试清空目录并新建 $1 MB 文件, 然后追加写 ${bs_size2}  (不超出配额)"
    
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size1} count=1";echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
    #test append
    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size2} count=1 oflag=append conv=notrunc";echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
    
    func_get_quota_used; new_used=${global_used}
    expect_used=$((${bs_size1} + ${bs_size2}))

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} == ${bs_size1})) && (( ${new_used} == ${expect_used})); then
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_append_exceed() {
    file_size=$1
    rm -rf ${WRITEDIR}/*
    bs_size1=$(($1 *1024 *1024)) #1 MB
    bs_size2=$((10 *1024 *1024)) #10 MB
    echo "测试清空目录并新建 $1 MB 文件, 然后追加写 ${bs_size2}  (超出配额)"
    
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size1} count=1";echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
    #test append
    func_get_quota_used; old_used=${global_used}
    cmd="dd if=/dev/zero of=${WRITEDIR}/file bs=${bs_size2} count=1 oflag=append conv=notrunc";
    echo -e "exec cmd:\n\t ${cmd}"; eval ${cmd} &> /dev/null
    
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
    if (( ${exceed} == 1 )); then echo -e "\t Error: Disk quota exceeded"; else echo -e "==${num}==\${msg$num}==== fail"; fi
    func_get_quota_used; new_used=${global_used}
    expect_used=${bs_size1}

    eval echo -n -e "==${test_num}==\${msg$test_num}===="
    if ((${old_used} == ${bs_size1})) && (( ${new_used} == ${expect_used})) && (( ${exceed} == 1 )); then
        echo -e "success exceed\n"; else echo -e "fail\n"; exit 1; fi
}




echo "/*******************************"
echo "      测试空配额目录写文件        "
echo "*******************************/"
source ${QUOTA_COMMON_FUNC}
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi

init_all 10; #MB
#set -x;
test_num=1; if (($1 == ${test_num})) || (($1 == 0));then test_create_newfile 1; fi #1 MB
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then test_create_newfile_exceed 12; fi #12 MB
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then test_open_with_trnuc 1; fi #1 MB
test_num=4; if (($1 == ${test_num})) || (($1 == 0));then test_open_with_trnuc_exceed 1; fi #1 MB
test_num=5; if (($1 == ${test_num})) || (($1 == 0));then test_append 1; fi #1 MB
test_num=6; if (($1 == ${test_num})) || (($1 == 0));then test_append_exceed 1; fi #1 MB

echo "/*******************************"
echo "    测试空配额目录写文件 全部成功 "
echo "*******************************/"
exit 0;

